package com.tianshi.pxx.controller.admin;



import com.tianshi.pxx.pojo.dto.UserDto;
import com.tianshi.pxx.pojo.entity.User;
import com.tianshi.pxx.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /*
    * 用户登录
    *
    * */
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody User user, HttpSession session) {
        log.info("login user:{}",user);
        User res=userService.login(user);
        log.info("login res:{}",res);
        if(res!=null)    {
            //保存到session,其实就是缓存到了redis中了
            session.setAttribute("currentUser",res); //登录成功，将用户id存入session
            return ResponseEntity.ok().body("success");
        }else{
            return ResponseEntity.badRequest().body("fail");
        }
    }

    /*
    * 用户注册
    * */
    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody UserDto userDto) {
        log.info("register user:{}", userDto);
        if(!userDto.getConfirmPassword().equals(userDto.getUser().getPassword())){
            return ResponseEntity.badRequest().body("两次密码输入不一致");
        }
        Integer res = userService.register(userDto.getUser());
        log.info("register res:{}", res);
        if (res != null) {
            return ResponseEntity.ok().body("success");
        } else {
            return ResponseEntity.badRequest().body("fail");
        }
    }

    /*
    * 检查用户是否在登录状态
    * */
    @GetMapping("/checkLogin")
    public ResponseEntity<User> checkLogin(HttpSession session) {
        log.info("checkLogin");
        User user=(User)session.getAttribute("currentUser");
        if(user!=null){
            return ResponseEntity.ok().body(user);
        }else{
            return ResponseEntity.badRequest().body(null);
        }
    }

    /*
    * 用户退出
    * */
    @PostMapping("/logout")
    public String logout(HttpSession session) {
        log.info("logout");
        session.removeAttribute("userId");  //退出登录
        redisTemplate.delete(redisTemplate.keys("*"));
        return "success";
    }


    /*
    * 根据id查询用户信息
    * */
    @GetMapping
    public ResponseEntity<User> getUserById(HttpSession session) {
        User user=(User)session.getAttribute("currentUser");
        log.info("getUserById user:{}", user);
        return ResponseEntity.ok().body(user);
    }

    /*
    * 根据id修改用户信息
    * */
    @PutMapping
    public ResponseEntity<String> updateUser(@RequestBody User user, HttpSession session) {
        log.info("updateUser user:{}", user);
        Integer res = userService.updateUser(user);
        if (res != null) {
            session.setAttribute("currentUser",user); //修改成功，更新session中的用户信息
            return ResponseEntity.ok().body("success");
        } else {
            return ResponseEntity.badRequest().body("fail");
        }
    }
}
